﻿using AutoMapper;
using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.ReadAPI.Applications.Commands.Users;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Meeting.ReadAPI.Applications.CommandHandlers.Users
{
    public class SelectUserCommandHandler : IRequestHandler<SelectUserCommand, APIResult<IList<User>>>
    {
        IMapper mapper;
        IRedisGetList<User> uRedis;
        IBaseRepository<User> uRep;
        ILogger<SelectUserCommandHandler> logger;

        public SelectUserCommandHandler(IBaseRepository<User> uRep, ILogger<SelectUserCommandHandler> logger, IMapper mapper, IRedisGetList<User> uRedis)
        {
            this.uRep = uRep;
            this.logger = logger;
            this.mapper = mapper;
            this.uRedis = uRedis;
        }

        public async Task<APIResult<IList<User>>> Handle(SelectUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                IList<User> userlist = await uRedis.GetList("GetUserList", async () =>
                {
                    var user = await uRep.GetAll().Where(x => !x.IsDel).ToListAsync();
                    return user;
                },60*60);
                return new APIResult<IList<User>>
                {
                    Code=StatusEnumCode.Ok,
                    Message="获取用户列表成功",
                    Data=userlist
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
